<?php
   class  ApiCenter_VendorsApi_Model_Warehouse_Api  extends  Mage_Api_Model_Resource_Abstract {
      
       private $details_filter=array(
           'sender_phone'=>array("require"),
           'sender_address'=>array('require',"en"),
           'sender_phone'=>array("require","num"),
           'sender_country'=>array("require"),
           'sender_province'=>array("require"),
           'sender_city'=>array("require"),
           'sender_company'=>array("require","en"),
           'sender_email'=>array("require"),
           'sender_contact'=>array("require","en"),
           'sender_postcode'=>array("require"),
           
       );
       private  $address_filter=array(
           'sender_name'=>array("require","en"),
           'paltform'=>array("require"),
       );
       private $defineAscll = array(43,45);
       
       protected function _tocheckFiled($filteredData){
           Mage::log($filteredData);
           $check_filter = array_merge($this->details_filter,$this->address_filter);
           $pattern = '/[^\x00-\x80]/';
           $isError ="";
           //validate
           if(!is_array($filteredData)){
               $this->_fault("仓库数据有误");
           }
            
           
           foreach ($check_filter as $filed => $v){
               if(!isset($filteredData[$filed])){
                   $isError.=$filed."|字段必须要填写;";
               }
           }
           foreach($filteredData as $key=>$val){
               foreach ($check_filter[$key] as $rule){
                   if($rule=="require"){
                       if($val==""){
                           $isError.=$key."|字段值不能为空;";
                       }
                   }
                   if($rule=="en"){
                       if(preg_match($pattern,$val)){
                           $isError.= $key."|字段必须为英文;";
                       }
                   }
                   if($rule=="num"){
                       $symbol =   substr($val, 0,1);
                       if(!in_array(ord($symbol), $this->defineAscll)&&!is_numeric($symbol)){
                           $isError.= $key."|字段首字母有误;";
                       }else{
                           if(!is_numeric($val)){
                               $isError.= $key."|字段只能为数字或带有指定符号的数字;";
                           }
                       }
                   }
               }
           }
           if(!empty($isError)){
              $this->_fault($isError);
           }
       }
       
       protected function _toSaveData($postData,$vendor,$id=0){

           if($id){
               $address_model =  Mage::getModel("vendorswarehouse/address")->load($id);
           }else{
               $address_model =  Mage::getModel("vendorswarehouse/address");
           }

           $VendorId =$vendor->getVendorId();
           $address_model-> setVendorId($vendor->getId())
                                   ->setSenderName($postData['sender_name'])
                                   ->setStatus($postData['status'])
                                   ->setPaltform($postData['paltform']);
       
           if(!$id){
               $head = substr($VendorId,0,strpos($VendorId,"S"));
               //设置仓库编码
               $Collection = Mage::getModel('vendorswarehouse/address')->getCollection()
               ->addFieldToFilter("sender_zip_code",array("like"=>$head."%"));
       
               $Collection->getSelect()
               ->columns(array("sender_zip_code"=>new Zend_Db_Expr('MAX(sender_zip_code)')));
               $maxCode= $Collection->getFirstItem()->getData("sender_zip_code");
       
       
               if(empty($maxCode)){
                   $startnum = strpos($vendor->getVendorId(),"S");
                   if($startnum ==false){
                       $this->_fault("仓库编码生成失败");
                   }
                   $laststr = substr($VendorId,$startnum+1);
                   $zipCode = ($head . "SW" . $laststr);
               }else{
                   $startnum = strpos($maxCode,"W");
                   if($startnum ==false){
                       $this->_fault("仓库编码获取失败");
                   }
                   $laststr = (int)substr($maxCode,$startnum+1);
                   $laststr+=1;
                   if(strlen($laststr)!=5){
                       $laststr =  str_pad($laststr,5,"0",STR_PAD_LEFT);
                   }
                   $zipCode = ($head . "SW" . $laststr);
               }
               $address_model->setSenderZipCode($zipCode);
       
           }
           //接口自动设置默认仓库地址 

               $vendor->setWarehouseCode($address_model->getSenderZipCode());
               $vendor->save();

       
           try {
               $address_model->save();
           } catch (Exception $e) {
               $this->_fault("仓库数据保存失败,请在试一次");
           }
            
           $addressId =$address_model->getEntityId();
           if(!$addressId) {
               $this->_fault("仓库地址信息保存失败");
           }
           if($id){
               $details_model =  Mage::getModel("vendorswarehouse/details")->load($id,"wid");
               $details_model->setUpdateTime(now());
           }else{
               $details_model = Mage::getModel("vendorswarehouse/details");
           }
           foreach($this->details_filter as $attr => $v){
               $details_model->setData($attr,$postData[$attr]);
           }
           $details_model->setWid($addressId);
           $details_model->save();
           
           return $address_model->getSenderZipCode();
       }
       
      
    }